function get_value_of_select (obj) { 
    var ret='';
    var nodes = obj.childNodes;
    var selected=obj.value;

    //So old school
    for (i=0; i < nodes.length; i++) {
        if (i==1) continue;
        val =nodes[i].innerHTML;
        id=nodes[i].value;
        if (val && id == selected) ret=val;
    }
    return ret;
    
}

function get_latitude_longitude (evt) { 
    if (! $F('id_name')) return;
    if (! $F('id_country')) return;
    
    v = $F('id_name') + "-" + get_value_of_select($('id_country'));
    var url="/travel/latitude_longitude/" + encodeURIComponent(v);

    new Ajax.Request(url, {
            method: 'get',
                onSuccess: function(transport) {
                result = transport.responseText;
                if (!result) return;
                hash=$w(result);
                $('id_latitude').value = hash[0];
                $('id_longitude').value = hash[1];
            }
    });
}

function main () { 
    fieldset=$('id_latitude').parentNode;
    var html = '<div class="form-row"><a id="id_get_latitude_longitude" href="javascript: void(0)">Get Latitude/Longitude</div>';
    elm = new Insertion.Before(fieldset, html);    
    Event.observe('id_get_latitude_longitude', 'click',  get_latitude_longitude);
}

Event.observe(window, 'load', main);
